<?php
/**
 * Author-> Gurdeep Singh <gurdeep@sfsu.edu, gurdeepsingh03@gmail.com>
 * Written for e-TAT (2010-04-02), SFSU, Department of Computer Science 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
  1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

$access_level=1;
$page_title = 'eTAT|Order Questions';

require_once '../../controller.php';

$survey_id = $_REQUEST['sid'];
$s = new Survey();
$q = new Question();

if(isset($_POST['move_up'])){
	$up_id = $_POST['change_order'];
	$o = $q->getOrder($up_id);
	
	$question_order = $o->order;
	$new_order = $question_order -1;
	if($question_order>1){
	while(true){	
	$upper_question = $q->getQuestionByOrder($survey_id,$new_order);
	if($upper_question){
		$q->updateOrder($up_id,$new_order);
		$q->updateOrder($upper_question->id,$question_order);
        break;	
	  }
	 $new_order = $new_order-1; 
	}
 }
	
header("location: order.php?sid=$survey_id&qid=$up_id");	
	
}

if(isset($_POST['move_down'])){
	$down_id = $_POST['change_order'];
	$o = $q->getOrder($down_id);
	
	$question_order = $o->order;
	$new_order = $question_order +1;
	
	$largest_order = $q->getLargestOrder($survey_id);
	if($question_order<$largest_order){
	while(true){	
	$bottom_question = $q->getQuestionByOrder($survey_id,$new_order);
	if($bottom_question){
		$q->updateOrder($down_id,$new_order);
		$q->updateOrder($bottom_question->id,$question_order);
        break;	
	  }
	 $new_order = $new_order+1; 
	}
 }
	
header("location: order.php?sid=$survey_id&qid=$down_id");	
	
}

$questions = $s->getQuestions($survey_id);
	if($questions){
	foreach ($questions as $i => $values) {
		$question_id[$i] = $values->id;
		$type[$i] = $values->type;
		$required[$i] = $values->required;
		$question_value[$i] = $values->question	;
		$create_section[$i] = $values->create_section;
		$order[$i] = $values->order;
		$response1[$i] = $values->response1;
		$response2[$i] = $values->response2;
		$response3[$i] = $values->response3;
		$response4[$i] = $values->response4;
		$response5[$i] = $values->response5;
		$response6[$i] = $values->response6;
	}
	$num = sizeof($type);
}

require_once '../../header.php';
echo "<br><br>";
echo "<h1 align='center'>Change Order of Question</h1>";
$survey = $s->getSurvey($survey_id);
$survey_name = $survey['name'];
echo "<h1 align='center'><u>Survey Name:</u> ".$survey_name."</h1>";
echo "<table align='right'>";
echo "<tr><td width='200px'><a href='question.php?sid=$survey_id'><input type='button' Value='Go Back To Questions'></a></td>";
//echo "<td width='200px'><a href='preview.php?sid=$survey_id'><input type='button' Value='Preview Survey'></a></td>";
//echo "<td width='200px'><a href='finish.php?sid=$survey_id'><input type='button' Value='Finish Survey'></a></td><tr>";
echo "</table>";
?>

<br></br>
<div id='order-info-display'>
<p><b><u>Question Order:</u></b> Questions are displayed in survey according to their order. Every question is assigned a order as you add/create them.
You can change the order by pressing either Move Up or Move Down Button </p><br></br>
<p><b><u>Move Up:</u></b>Move Up changes the order of question upwards in the list. </p><br></br>
<p><b><u>Move Down:</u></b>Move Down changes the order of question downwards in the list. </p><br></br>
</div>
<h1><strong>Change Order of Questions</strong></h1>
<form name='display' method='post' action='order.php?sid=<?php echo $survey_id; ?>'>
<select name="change_order" style="width: 500px" size="12">
<?php 
    $question_num =1;
    for($i=0;$i<$num;$i++){
        	
	if($create_section[$i]=='Y'){  
	echo "<option  disabled='disabled'><-------section break/ new section-------></option>";
	}
	if($type[$i]=='Section Text'){
	echo "<option value='$question_id[$i]'><strong>SECTION TEXT.</strong>$question_value[$i]</option>";
	}else{
	echo "<option value='$question_id[$i]'>$question_num".". " ."$question_value[$i]</option>";
	$question_num++;
	}
}	
echo "</select><br><br>";
echo "<input type='submit' name='move_up' Value='Move Up'>";
echo "<input type='submit' name='move_down' Value='Move Down'>";
echo "</form><br><br>";	

echo "<div class='clear'></div>";
require_once '../../footer.php';
?>